//
//  MyLayout.m
//  UI26_自定义layout
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MyLayout.h"

@interface MyLayout ()
//原理:每次找到当前的最短列 设置cell
@property(nonatomic, strong)NSMutableDictionary *columnYDic;/**< 保存每列Y值*/
@property(nonatomic, strong)NSMutableArray *layoutArr;/**< 保存计算出的cell布局*/
@end

@implementation MyLayout

-(instancetype)init
{
    self = [super init];
    if (self) {
        self.columnYDic = [NSMutableDictionary dictionary];
        self.layoutArr = [NSMutableArray array];
    }
    return self;
}

//布局item时  执行的第一个方法(准备布局)
-(void)prepareLayout
{
    [super prepareLayout];
    //初始化Y值字典 根据列数创建key-value
    for (NSInteger i = 0; i < _columnCounts; i++) {
        NSString *key = [NSString stringWithFormat:@"%ld",i];
        //value(初始Y值)
        _columnYDic[key] = @(_edgeInsets.top);
        //[_columnYDic setObject:@(_edgeInsets.top) forKey:key];
    }
    //获取item个数
    NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
    //遍历计算每一个item的frame
    for (NSInteger i = 0; i < itemCount; i++) {
        //通过下标计算
        [self itemFrameWithIndex:i];
    }
}

///计算frame
-(void)itemFrameWithIndex:(NSInteger)index
{
    //找到最短列 计算frame
    //定义临时变量 保存最短列下标
    NSString *minC = @"0";
    //遍历Y值字典
    for (NSString *key in _columnYDic) {
        //找到最短列
        //最短下标对应的Y值
        CGFloat min = [_columnYDic[minC] doubleValue];
        //当前下标对应Y值
        CGFloat cur = [_columnYDic[key] doubleValue];
        if (min > cur) {
            minC = key;
        }
    }
    //计算frame
    //宽度 =(总宽-左边-右边-(列数-1)*列间)/列数
    CGFloat w = (self.collectionView.bounds.size.width - _edgeInsets.left - _edgeInsets.right - (_columnCounts - 1) *_columnSpace) / _columnCounts;
    //x = 左边 + (宽+列间) *最小列号
    CGFloat x = _edgeInsets.left + (w + _columnSpace) * minC.doubleValue;
    //高度=图高/图宽 *cell宽
    //创建位置对象
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    //高度计算协议方法
    //参数1:当前layout对应的collectionView
    //参数2.每一个item的位置对象
    //参数3.item宽
    CGFloat h = [self.delegate collectionView:self.collectionView heightForItemAtIndexPath:indexPath byWidth:w];
    //更新字典Y值 得到最短列
    //最短列Y值
    CGFloat y =[_columnYDic[minC] doubleValue];
    //更新Y
    //新Y = 旧Y+高+行间
    [_columnYDic setObject:@(y + h + _rowSpace) forKey:minC];
    
    //创建布局对象
    UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    //添加frame
    attr.frame = CGRectMake(x, y, w, h);
    [_layoutArr addObject:attr];

}

//布局方法
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return _layoutArr;
}

//滚动范围
- (CGSize)collectionViewContentSize
{
    //找到最长列 滚动范围>=最长咧
    NSString *maxC = @"0";
    //遍历Y值字典
    for (NSString *key in _columnYDic) {
        //找到最短列
        //最短下标对应的Y值
        CGFloat max = [_columnYDic[maxC] doubleValue];
        //当前下标对应Y值
        CGFloat cur = [_columnYDic[key] doubleValue];
        if (max < cur) {
            maxC = key;
        }
    }
    //内容高度
    CGFloat maxH = [_columnYDic[maxC] doubleValue] +_edgeInsets.bottom;
    return CGSizeMake(0, maxH);
}

@end
